package com.kzd.gtgame.jetpack.manager

import com.kzd.gtgame.app.*
import com.kzd.gtgame.base.BaseManager
import com.kzd.gtgame.entity.*
import com.kzd.gtgame.main.home.adapter.HOME_FEATURED_TYPE_100
import com.module.lemlin.http.HttpResponseData
import com.module.lemlin.http.HttpResponseDataE
import com.module.lemlin.http.HttpResponseDataM
import com.module.lemlin.http.HttpResponseDataT
import rxhttp.RxHttp
import rxhttp.await

class IndexRepository : BaseManager() {


//    /**
//     * 首页轮播图
//     * */
//    suspend fun homeBanner(ios: Int): HomeBannerResponse = RxHttp.postForm(NEW_APPS_GET_BANNER)
//        .add("ios", ios)
//        .await()
//
//    /**
//     * 首页
//     * */
//    suspend fun homeType(): HomeIndexResponse = RxHttp.postForm(NEW_APPS_INDEX)
//        .await<HomeIndexResponse>()
//        .apply {
//            val list = data.filter {
//                it.type != 0 && it.list.isNullOrEmpty()
//            }
//            data.removeAll(list)
//        }

    /**
     * 合并首页+轮播图接口
     * */
    suspend fun homeIndex(): HttpResponseDataE<HomeIndex> {
        val homeBannerAnyResponse = RxHttp.postForm(NEW_APPS_GET_BANNER)
            .add("ios", 0)
            .await<HttpResponseDataE<Any>>()
        val homeIndex = HomeIndex(
            type = HOME_FEATURED_TYPE_100, list = homeBannerAnyResponse.data
        )
        return RxHttp.postForm(NEW_APPS_INDEX)
            .await<HttpResponseDataE<HomeIndex>>()
            .apply {
                data.add(0, homeIndex)
                val list = data.filter {
                    it.type != 0 && it.list.isNullOrEmpty()
                }
                data.removeAll(list)
            }
    }

    /**
     * 新游首发-今日首发
     *
     * @param start
     * */
    suspend fun travelToday(start: String): HttpResponseDataT<Travel<GameGroup>> =
        RxHttp.postForm(NEW_APPS_SHOUFA_LIST)
            .add("start", start)
            .await()

    /**
     * 新游首发-一周新游top10
     * */
    suspend fun travelWeek(): HttpResponseDataE<GameBean> =
        RxHttp.postForm(NEW_APPS_SHOUFA_WEEK_LIST)
            .await()

    /**
     * 新游首发-新游预约
     * */
    suspend fun travelReserve(
        start: String,
        page: String,
        limit: String,
    ): HttpResponseDataT<Travel<GameReserve>> =
        RxHttp.postForm(NEW_APPS_YUYUE)
            .add("start", start)
            .add("p", page)
            .add("limit", limit)
            .await()

    /**
     * 新游首发-新游预约修改
     * */
    suspend fun reserveChange(gid: String, type: String): ReserveChangeResponse =
        RxHttp.postForm(NEW_APPS_YUYUE_CHANGE)
            .add("gid", gid)
            .add("type", type)
            .await()

    /**
     * 活动数据
     * */
    suspend fun homeDoings(
        gameLayout: String, start: String, page: String, limit: String,
    ): HttpResponseDataT<Doings<GameDoing>> = RxHttp.postForm(NEW_APPS_GAME_LAYOUT1)
        .add("game_layout", gameLayout)
        .add("start", start)
        .add("p", page)
        .add("limit", limit)
        .await()

    /**
     * 免费领券
     * */
    suspend fun homeCoupon(
        keyword: String? = null, type: String, page: String, limit: String,
    ): HttpResponseDataE<GameCoupon> = RxHttp.postForm(NEW_APPS_VOUCHER_YOUXI)
        .add("keyword", keyword)
        .add("type", type)
        .add("p", page)
        .add("limit", limit)
        .await()


    /**
     * 账号交易-最新发布
     * */
    suspend fun homeBargainRelease(
        keyword: String, sort: String, system: String, page: String, limit: String,
    ): HttpResponseDataE<GameBargain> = RxHttp.postForm(PERSONAL_TRADE)
        .add("keyword", keyword)
        .add("sort", sort)
        .add("system", system)
        .add("p", page)
        .add("limit", limit)
        .await()

    /**
     * 账号交易-官方精选
     * */
    suspend fun homeBargainFeatured(): HttpResponseDataE<GameBargain> =
        RxHttp.postForm(PERSONAL_TRADE_JINGXUAN)
            .await()

    /**
     * 游戏礼包
     * */
    suspend fun homePresent(
        keyword: String, type: String, page: String, limit: String,
    ): HttpResponseDataE<GamePresent> = RxHttp.postForm(NEW_APPS_LB)
        .add("keyword", keyword)
        .add("type", type)
        .add("p", page)
        .add("limit", limit)
        .await()


    /**
     * 游戏开服-今日开服
     * */
    suspend fun homeServiceToday(): ServiceTitleResponse {

        val todayResponse = RxHttp.postForm(NEW_APPS_GET_TODAY_KF)
            .await<HomeServiceTodayResponse>()
        val data = todayResponse.data

        val titles = mutableListOf<ServiceTitle>()
        for ((key, value) in data.list) {
            titles.add(ServiceTitle(title = key, list = value))
        }
        return ServiceTitleResponse(
            data = titles
        ).apply {
            times = data.times
            code = todayResponse.code
            message = todayResponse.message
        }
    }

    /**
     * 游戏开服-即将开服
     * */
    suspend fun homeServiceQuick(): HttpResponseDataM<String, MutableList<ServiceTitle>> {

        val quickResponse = RxHttp.postForm(NEW_APPS_ALL_KF_LIST)
            .await<HttpResponseDataM<String, ServiceTime>>()
        val data = quickResponse.data

        val quickMaps = mutableMapOf<String, MutableList<ServiceTitle>>()
        for ((key, value) in data) {
            val titles = mutableListOf<ServiceTitle>()
            for ((key1, value1) in value.list) {
                titles.add(ServiceTitle(title = key1, list = value1))
            }
            quickMaps[key] = titles
        }
        return HttpResponseDataM(
            data = quickMaps
        ).apply {
            code = quickResponse.code
            message = quickResponse.message
        }

    }

    /**
     * 游戏开服-历史开服
     * */
    suspend fun homeServiceHistory(
        page: String, limit: String,
    ): HttpResponseDataE<GameService> = RxHttp.postForm(NEW_APPS_KF)
        .add("p", page)
        .add("limit", limit)
        .await()

    /**
     * 游戏抽奖数据
     * */
    suspend fun homeLottery(): HttpResponseDataT<GameLottery> =
        RxHttp.postForm(PERSONAL_LOTTERY_DATA)
            .await()

    /**
     * 游戏抽奖
     * @param type 0抽1次，1抽10次
     * */
    suspend fun homeLotteryObtain(type: String): HttpResponseDataE<LotteryObtain> =
        RxHttp.postForm(PERSONAL_LOTTERY_OBTAIN)
            .add("type", type)
            .await()

    /**
     * 游戏抽奖记录
     * */
    suspend fun homeLotteryRecord(page: String, limit: String): HttpResponseDataE<LotteryRecord> =
        RxHttp.postForm(PERSONAL_LOTTERY_RECORD)
            .add("p", page)
            .add("limit", limit)
            .await()
}